

import 'package:date_format/date_format.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:smart_freezer/core/model/product_model.dart';
import 'package:smart_freezer/core/resources/app_config.dart';
import 'package:smart_freezer/core/resources/icon_names.dart';
import 'package:smart_freezer/core/resources/routers.dart';
import 'package:smart_freezer/core/service/api/app_api.dart';
import 'package:smart_freezer/core/service/service/result_data.dart';
import 'package:smart_freezer/core/utils/color_util.dart';
import 'package:smart_freezer/core/utils/local_storage.dart';
import 'package:smart_freezer/core/utils/logs.dart';
import 'package:smart_freezer/core/utils/tr_extension.dart';
import 'package:smart_freezer/generated/locale_keys.g.dart';

class DeviceInfo extends StatefulWidget {
  const DeviceInfo({Key? key}) : super(key: key);

  @override
  State<DeviceInfo> createState() => _DeviceInfoState();
}

class _DeviceInfoState extends State<DeviceInfo> {

  final _productId = Get.arguments as String;
  ProductModel _productModel = ProductModel();

  List _dataList = [
    {"title": LocaleKeys.device_info_xuni_id.trc(), "content": "1235555"},
    {"title": LocaleKeys.device_info_custom_name.trc(), "content": "冷柜编号+冷柜型号"},
    {"title": LocaleKeys.device_info_device_address.trc(), "content": "1楼"},
    {"title": "", "content": ""},
    {"title": LocaleKeys.device_info_device_xinghao.trc(), "content": "HGIT"},
    {"title": LocaleKeys.device_info_device_bianhao.trc(), "content": "xxxxxx"},
    {"title": LocaleKeys.device_info_device_xuliehao.trc(), "content": "xxxxxx"},
    {"title": LocaleKeys.device_info_device_jingxiao.trc(), "content": "经销商"},
    {"title": "", "content": ""},
    {"title": LocaleKeys.device_info_wenkong_xinghao.trc(), "content": "xxxx"},
    {"title": LocaleKeys.device_info_wenkong_status.trc(), "content": "报警"},
    {"title": LocaleKeys.other_warn_info.trc(), "content": "xx"},
    {"title": "", "content": ""},
    // {"title": "原厂保修期限", "content": "2023-03-30"},
  ];

  bool isDarkMode = LocalStorage.getThemeMode() == 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _getDeviceInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // backgroundColor: Colors.white,
        elevation: 1,
        leadingWidth: 50,
        leading: InkWell(
          onTap: () {
            Get.back();
          },
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 9, 0, 9),
            child: Icon(Icons.arrow_back_ios_rounded),
          ),
        ),
        title: Text(LocaleKeys.device_info_device_info.trc(),),
        centerTitle: true,
      ),
      body: Container(
        color: isDarkMode ? null : HexColor("#F1F3F8"),
        width: MainCommon.screenWidth,
        padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
        child: ListView.builder(
            itemCount: _dataList.length,
            itemBuilder: (context, index){
              final title = _dataList[index]["title"] as String;
              final content = _dataList[index]["content"];

              return title.isNotEmpty ? Container(
                color: isDarkMode ? MainCommon.darkBlueMaterialColor : Colors.white,
                // height: 50,
                constraints: BoxConstraints(
                  minHeight: 50
                ),

                child: Column(
                  children: [
                    Container(
                      // height: 49,
                      // color: Colors.white,
                      constraints: BoxConstraints(
                          minHeight: 50
                      ),
                      padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(title, style: TextStyle(fontSize: 16),),
                          Container(
                            constraints: BoxConstraints(
                              maxWidth: 200,
                            ),
                            child: Text(content , style: TextStyle(fontSize: 15, color: Colors.grey,), textAlign: TextAlign.right,),
                          ),
                        ],
                      ),
                    ),
                    Divider(
                      height: 1,
                      indent: 5,
                      endIndent: 5,
                    ),
                  ],
                ),
              ) : SizedBox(height: 10,);
        }),
      ),
    );
  }

  // 获取设备数据
  _getDeviceInfo() async {
    final params = {"productId": "${_productId}"};
    ResultData data = await AppApi.getInstance().getProductInfo(params);
    if(data.code == 0){
      _productModel = ProductModel.fromJson(data.data);

      final time = DateTime.parse(_productModel.maintenanceTime);
      final maintenanceTime = formatDate(time, [yyyy, '-', mm, '-', dd]);

      _dataList = [
        {"title": LocaleKeys.device_info_xuni_id.trc(), "content": _productModel.devId},
        {"title": LocaleKeys.device_info_custom_name.trc(), "content": "${_productModel.name}"},
        {"title": LocaleKeys.device_info_device_address.trc(), "content": "${_productModel.hallName} ${_productModel.floorName}"},
        {"title": "", "content": ""},
        {"title": LocaleKeys.device_info_device_xinghao.trc(), "content": _productModel.modelName},
        {"title": LocaleKeys.device_info_device_bianhao.trc(), "content": _productModel.code},
        {"title": LocaleKeys.device_info_device_xuliehao.trc(), "content": _productModel.serialNumber},
        {"title": LocaleKeys.device_info_device_jingxiao.trc(), "content": _productModel.agencyName},
        {"title": "", "content": ""},
        {"title": LocaleKeys.device_info_wenkong_xinghao.trc(), "content": _productModel.controllerModelName},
        {"title": LocaleKeys.device_info_wenkong_status.trc(), "content": _productModel.controlStatus},
        {"title": LocaleKeys.other_warn_info.trc(), "content": _productModel.warningMessage},
        {"title": "", "content": ""},
        // {"title": "原厂保修期限", "content": maintenanceTime},
      ];

      logs("$_dataList");

      setState(() {});
    }else {
      SmartDialog.showToast(data.msg);
    }
  }
}
